package com.hnxxxy.common.redis;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @CLASSDESCRIPER:
 * @AUTHOR:suhui
 * @PHONE:13319575368
 * @EMIAL:13319575368@163.COM
 * @DATE: 2020/5/21
 * @PARAMDESCRIPER:
 * @OUTPARAMDESCRIPER:
 */
@Configuration
@Component

public class RedisConfig {

    @Value("#{'${spring.redis.sentinel.nodes}'.split(',')}")
    private List<String> nodes;

    @Value("${spring.redis.sentinel.master}")
    private String msatername;

    @Value("${spring.redis.pwd}")
    private String pwd;

    @Bean("redisPool")
    public JedisPoolConfig getJedisPoolConfig(){
        JedisPoolConfig poolConfig=new JedisPoolConfig();
        poolConfig.setMaxIdle(1);
        poolConfig.setMinIdle(1);
        poolConfig.setMaxTotal(5);
        poolConfig.setLifo(true);
        poolConfig.setBlockWhenExhausted(true);
        poolConfig.setMaxWaitMillis(30000);
        poolConfig.setTestOnBorrow(false);
        poolConfig.setTestWhileIdle(false);
        poolConfig.setSoftMinEvictableIdleTimeMillis(60000);
        poolConfig.setTimeBetweenEvictionRunsMillis(30000);
        poolConfig.setNumTestsPerEvictionRun(3);
        return poolConfig;
    }

    @Bean("sentinelconfig")
    public RedisSentinelConfiguration getRedisSentinelConfiguration(){
        /*nodes.add("10.32.19.56:28082");
        nodes.add("10.32.19.56:28083");
        nodes.add("10.32.19.56:28084");*/
        RedisSentinelConfiguration redisSentinelConfiguration=new RedisSentinelConfiguration();
        redisSentinelConfiguration.master(msatername);
        //配置redis的哨兵sentinel
        Set<RedisNode> redisNodeSet = new HashSet<>();
        nodes.forEach(x->{
            redisNodeSet.add(new RedisNode(x.split(":")[0],Integer.parseInt(x.split(":")[1])));
        });
        redisSentinelConfiguration.setSentinels(redisNodeSet);
        return redisSentinelConfiguration;
    }

    @Bean("redisFactory")
    public JedisConnectionFactory jedisConnectionFactory(@Qualifier("redisPool")JedisPoolConfig jedisPoolConfig, @Qualifier("sentinelconfig")RedisSentinelConfiguration sentinelConfig) {
       JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig,jedisPoolConfig);
        jedisConnectionFactory.setPassword(pwd);
        return jedisConnectionFactory;

    }

    @Bean("redisTemplate")
    public RedisTemplate<String,Object> redisTemplate(@Qualifier("redisFactory") JedisConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
        template.setStringSerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericFastJsonRedisSerializer());
        return template;
    }

    @Bean("redisTemplate1")
    public RedisTemplate<String,Object> redisTemplate1(@Qualifier("redisFactory") JedisConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        return template;
    }


}
